Event.observe(window, 'load', function() {
	bindUserRows();
});

function bindUserRows() {
	if ($('userstable')) {
		$('userstable').getElementsByClassName('row-').each(function (item) {
			Event.observe(item, 'mouseover', selectRowName.bindAsEventListener(item));
			Event.observe(item, 'mouseout',  unselectRowName.bindAsEventListener(item));
			
			approver = item.getElementsByClassName('approve-')[0].getElementsByTagName('a')[0];
			if (approver) Event.observe(approver, 'click', rowApprove.bindAsEventListener(item));

			decliner = item.getElementsByClassName('decline-')[0].getElementsByTagName('a')[0];
			if (decliner) Event.observe(decliner, 'click', rowDecline.bindAsEventListener(item));
			
		})
	}
}

function unselectRowName(event) {
	if (!Event.element(event).hasClassName('selector-')) {
	    this.removeClassName("row-selected-");
	    this.addClassName("row-");
	} else {
	    Event.stop(event);
	}
}
function selectRowName(event) {

	if (this.hasClassName('row-'))
	{
		this.removeClassName("row-");
		this.addClassName("row-selected-");
	}
}

function rowDecline(event) {
	Event.stop(event);
	var user_id = this.getElementsByClassName('userid-')[0].value;
	
	obj = this;
	var cp = new cpaint();
	
	if (confirm('Вы действительно хотите отклонить заявку этого пользователя?')) {
		cp.call( 
			jq_submit_url, 
			'ajax_doDeclineUser', 
			function (result) { 
				var r = result.ajaxResponse['0'];
				var code = r.status['0'].data;
				switch (code) {
				    case '0':
					    obj.remove();
					    break;
				    default:
						alert(r.data['0'].data);
					    obj.removeClassName('changed-'); 
					    obj.addClassName('error-'); 
				}
		}, 
		user_id
		);
		
	}
	
}


function rowApprove(event) {
	//this.visualEffect('highlight', {duration:1.5})
	Event.stop(event);
	var user_id = this.getElementsByClassName('userid-')[0].value;

	obj = this;
	var cp = new cpaint();
	cp.call( 
		jq_submit_url, 
		'ajax_doApproveUser', 
		function (result) { 
			var r = result.ajaxResponse['0'];
			var code = r.status['0'].data;
			switch (code) {
			    case '0':
				    obj.remove();
				    break;
			    default:
				alert(r.data['0'].data);
				    obj.removeClassName('changed-'); 
				    obj.addClassName('error-'); 
			}
	}, 
	user_id
	);
}
